<template>
<view>
<view class="container">
	<view class="info-card">
		<input type="text" auto-focus="true" @input="checkInputLength" class="black-border-input" v-model="inputValue"/>
	</view> 
	 <!-- <view>{{ output }}</view> -->	 
    <!-- <button @click="startScan">入库扫码</button> -->
	<!-- 确定按钮 -->
  </view>
  <view class="button-container1">
	<button @click="shoudong">扫描随货同行单</button>
    <button @click="goback">返回</button>
  </view>
</view>
</template>

<script>

import { ref } from 'vue'
import * as SupplierSendOrderApi from '@/api/suppliersendorder/inde.js'; // 随货同行单
import * as purchaseinorderApi from '@/api/purchaseinorder/index.js'; // 采购入库审批单

export default {
  data() {
	  return {
		  		 inputValue: '' ,// 添加一个数据属性来存储输入框的值
		  		output: '',
		  		purchaseinorderResult: {}
	  };
  },
  onLoad() {
    // this.startScan(); 
  },
  methods: {
	  	  checkInputLength(event) {
	  	    if (event.target.value.length > 10) {
	  			const inputValue = event.target.value;
	  			console.log('输入框的值:', inputValue); 
	  			this.handleScanResult(inputValue);
	  	  	  //扫描后逻辑
	  	      this.output = inputValue;
	  	    } else {
	  	      this.output = '';
	  	    }
	  	  },
	  startScan() {
	    uni.scanCode({
	      success: (res) => {
	        this.handleScanResult(res.result.trim());
	      },
	      fail: (err) => {
	        console.error('扫描失败:', err);
	  				console.Console('扫描失败:', err);
	      }
	    });
	  },
	  	      shoudong(){
	        // 弹出手动输入的模态对话框
	        uni.showModal({
	          title: '手动输入',
	          content: '',
	          placeholderText: '请输入内容',
	          editable: true, // 允许输入
	          success: (res) => {
	            if (res.confirm) {
	              // 用户点击确定按钮，获取输入的内容
	              const userInput = res.content;
	              this.inputValue = userInput; // 将输入的内容赋值到 inputValue
	              this.checkInputLength({ target: { value: userInput } }); // 调用 checkInputLength 方法
	            }
	          }
	        })},


async handleScanResult(scanData) {
  console.log('扫描结果', scanData);
  // 现根据扫描出的随货同行单id，查询随货同行单判断这批药品是否为当前科室的
  const ress = await SupplierSendOrderApi.getSupplierSendOrderByDepartmentId(scanData);
  // 判断返回数据是否为空或是否属于当前科室
  if (!ress.data) {
    uni.showToast({
      title: '不是当前部门的药品！',
      icon: 'none',
      duration: 3000
    });
    return;
  }
  // 根据随货同行单id 查入库审批单，看是否有数据。一个随货同行单不能重复扫描
   const purchaseOrderRes = await purchaseinorderApi.getPurPurchaseOrderByOutOrderId(scanData);
   console.log('purchaseOrderRes:',purchaseOrderRes.data)
    //判断是否为非空对象：存在且不是 null
	if (purchaseOrderRes.data) {
		if (purchaseOrderRes.data.pushStatus === 1) {  //已推送
			uni.showToast({
			  title: '该随货同行单已推送，不能重复扫描！',
			  icon: 'none',
			  duration: 3000
			});
			return; // 已扫描，终止执行
		} 
	}  
     
  // 跳转暂时拿出来测试
  uni.navigateTo({
    url: `/pages/scanCodes/login?data=${encodeURIComponent(scanData)}`
  });
},
async goback(){
	uni.navigateBack();
},
  },
};
</script>

<style>
.container {
  padding: 20px;
  background-color: #f5f5f5;
}

.info-card {
  background-color: #ffffff;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 15px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.info-card.horizontal {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: space-between;
}

.item-field {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.label {
  font-size: 15px;
  color: #333333;
  margin-bottom: 5px;
}

.value {
  font-size: 13px;
  color: #007aff;
}

.button-container {
  padding-top: 20px;
  text-align: center;
}

.button-container1 {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #ffffff;
  padding: 5px 0;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  text-align: center;
}
button {
  margin: 5px 5px;
}
</style>